package hmis.pos.Service.ServiceImpl;

import hmis.pos.Base.BaseService.Impl.BaseServiceImpl;
import hmis.pos.Common.pojo.ApiResponse;
import hmis.pos.Common.vo.CheckIn.OrderDetailAndRoomid;
import hmis.pos.Entity.CheckIn;
import hmis.pos.Entity.Order;
import hmis.pos.Entity.OrderDetail;
import hmis.pos.Entity.RoomMessage;
import hmis.pos.Repository.CheckInRepository;
import hmis.pos.Service.CheckInService;
import hmis.pos.Service.OrderDetailService;
import hmis.pos.Service.OrderService;
import hmis.pos.Service.RoomMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static hmis.pos.Common.Constant.OtherConstant.ORDERDETAIL_ARRANGED;
import static hmis.pos.Common.Constant.OtherConstant.ORDERDETAIL_COMPLETED;
import static hmis.pos.Common.Constant.OtherConstant.ORDERDETAIL_WAIT;

@Service
public class CheckInServiceImpl extends BaseServiceImpl<CheckIn,CheckInRepository> implements CheckInService {

    @Autowired
    private RoomMessageService roomMessageService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CheckInRepository checkInRepository;

    @Override
    public ApiResponse<OrderDetail> save(OrderDetailAndRoomid orderDetailAndRoomid) {

        OrderDetail orderDetail = orderDetailAndRoomid.getOrderDetail();
        Order order = orderDetail.getOrder();
        List<Integer> roomidList = orderDetailAndRoomid.getRoomidList();

        //判断是否为空
        if(orderDetailAndRoomid==null)return new ApiResponse<OrderDetail>(0 ,"选择数据为空！");

        //判断订单是否付款
        if(orderDetail.getOrder().getStatus().equals("未付款"))return new ApiResponse<OrderDetail>(0,"订单未付款！");

        //判断订单明细是否已安排
        if(!orderDetail.getStatus().equals(ORDERDETAIL_WAIT))return new ApiResponse<OrderDetail>(0,"订单不是未安排！");

        //判断选择的房间数量是否与预订数量相符
        if(roomidList.size()!=orderDetail.getRoomnumber())return new ApiResponse<OrderDetail>(0,"房间数量与预订数量不符！");

        //判断订单是否进行中
        String status = orderDetail.getOrder().getStatus();
        if(status.equals("已取消")||status.equals("未付款"))return new ApiResponse<OrderDetail>(0,"订单已取消或未付款！");

        //根据房间号查找房间信息生成房间信息List
        List<RoomMessage> roomMessageList = new ArrayList<>();
        for(int roomid:roomidList){
            RoomMessage roomMessage = roomMessageService.findByRoomid(roomid);
            roomMessageList.add(roomMessage);
        }

        //判断房间当前状态是否为空闲
        for(RoomMessage roomMessage:roomMessageList){
            if(!roomMessage.getStatus().equals("空闲"))return new ApiResponse<OrderDetail>(0,"房间状态不为空闲！");

            //判断选择房间类型是否与预订房间类型相同
            if(!roomMessage.getType().equals(orderDetail.getType()))return new ApiResponse<OrderDetail>(0,"选择房间类型与预订房间类型不同！");
        }

        //添加房间入住信息
        String orderdetail_id = orderDetail.getId();
        for(RoomMessage roomMessage:roomMessageList){
            int roomid = roomMessage.getRoomid();

            //获取当前时间
            Date today = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date());
            try {
                today = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //保存入住信息
            CheckIn checkIn = new CheckIn();
            checkIn.setRoomid(roomid);
            checkIn.setIn_time(today);
            checkIn.setOrderdetail_id(orderdetail_id);
            checkInRepository.save(checkIn);

            //修改房间状态
            roomMessage.setStatus("已入住");
            roomMessageService.update(roomMessage);
        }

        //修改订单详情状态
        orderDetail.setStatus(ORDERDETAIL_ARRANGED);//已安排
        orderDetailService.update(orderDetail);

        //修改订单状态
        if(!order.getStatus().equals("进行中"))order.setStatus("进行中");
        orderService.update(order);

        return new ApiResponse<OrderDetail>(1,"添加入住信息成功！",orderDetail);
    }

    @Override
    public List<CheckIn> findByRoomid(int roomid) {
        return checkInRepository.findByRoomid(roomid);
    }

    @Override
    public ApiResponse<CheckIn> checkOut(CheckIn checkIn) {

        OrderDetail orderDetail = checkIn.getOrderDetail();
        Order order = orderDetail.getOrder();
        RoomMessage roomMessage = roomMessageService.findByRoomid(checkIn.getRoomid());

        List<OrderDetail> orderDetailList = orderDetailService.findByOrder(order);

        //描述信息
        String message = "";

        //获取当前时间,转化为yyyy-MM-dd格式
        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        try {
            today = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //获取入住时间，转化为yyyy-MM-dd格式
        Date in_time = checkIn.getIn_time();
        try {
            in_time = sdf.parse(sdf.format(checkIn.getIn_time()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //比较日期，如果today早于入住时间
        if(today.before(in_time))return new ApiResponse<CheckIn>(0,"今天日期错误！");


        //添加退房日期,设置房间状态
        checkIn.setOut_time(today);
        roomMessage.setStatus("空闲");

        //判断该订单明细安排的房间是否都退房
        boolean isAllRoomDone = false;
        List<CheckIn> checkInList = this.findByOrderDetail(orderDetail);
        int number = 0;
        for(CheckIn checkIn1 : checkInList){
            if(checkIn1.getOut_time()==null)number++;//计算未退房间数
        }
        number--;
        if(number<=0)isAllRoomDone = true;
        else isAllRoomDone = false;

        //退房时间与今天日期比较-----------------------------订单状态未改变
        Date out_time = order.getOut_time();
        if(today.before(out_time)){
            orderDetail.setStatus(ORDERDETAIL_WAIT);
            message = "订单明细待安排！";
        }else if(today.after(out_time)&&isAllRoomDone){   //退房时间超过预订退房时间---待考虑
                orderDetail.setStatus(ORDERDETAIL_COMPLETED);
                order.setStatus("已完成");

        } else if(isAllRoomDone) {
            orderDetail.setStatus(ORDERDETAIL_COMPLETED);

            //循环判断是否所有订单明细都是已完成
            boolean isDone = false;
            for(OrderDetail orderDetail1:orderDetailList){
                if(ORDERDETAIL_COMPLETED.equals(orderDetail1.getStatus())||orderDetail1.getId().equals(orderDetail.getId()))isDone=true;
                else{
                    isDone = false;break;
                }
            }
            if(isDone)order.setStatus("已完成");
            else order.setStatus("进行中");
        }

        //更新数据库的数据
        roomMessageService.update(roomMessage);
        orderDetailService.update(orderDetail);
        orderService.update(order);
        CheckIn newCheckin = checkInRepository.save(checkIn);

        return new ApiResponse<CheckIn>(1,"退房成功！"+message,newCheckin);
    }

    @Override
    public List<CheckIn> findByOrder(Order order) {
        List<OrderDetail> orderDetailList = orderDetailService.findByOrder(order);
        List<CheckIn> checkInList = new ArrayList<>();

        for(OrderDetail orderDetail : orderDetailList){
            checkInList.addAll(this.findByOrderDetail(orderDetail));
        }

        return checkInList;
    }

    @Override
    public List<CheckIn> findByOrderDetail(OrderDetail orderDetail) {
        return checkInRepository.findByOrderDetailId(orderDetail.getId());
    }
}
